#fromkeys是一个类方法
dict1 = dict.fromkeys(range(10),['one','two','three'])
print(dict1)

dict2 = {
    1:'own',
    2:'she',
    3:'san',
    4:'sfjll',
    }
a = dict2.keys()
print(type(a))

b = dict2.get(5)

#setdefault方法
dict3 = {
    3:'tony',
    5:'tom',
    7:'jelly'
}

c = dict3.setdefault(3,'ifrr')
print(c)
c = dict3.setdefault(4,'ifrr')
print(dict3)

#defaultdict
from collections import defaultdict

default_dict_1 = defaultdict(list)
for i in range(10):
    default_dict_1[3].append(i)
print(default_dict_1)


# __missing__方法

#      Tests for item retrieval using `d[key]` notation::
#          >>> d = StrKeyDict0([('2', 'two'), ('4', 'four')])
#          >>> d['2']
#          'two'
#          >>> d[4]
#          'four'
#          >>> d[1]
#          Traceback (most recent call last):
#            ...
#          KeyError: '1'
#      Tests for item retrieval using `d.get(key)` notation::
#          >>> d.get('2')
#          'two'
#          >>> d.get(4)
#          'four'
#          >>> d.get(1, 'N/A')
#          'N/A'
#      Tests for the `in` operator::
#          >>> 2 in d
#          True
#          >>> 1 in d
#          False
class StrKeyDict0(dict):

    def __missing__(self,key):
        if isinstance(key,str):
            raise KeyError(key)
        return self[str(key)]


    def __contains__(self,key):
        if key in self.keys() or str(key) in self.keys():
            return True
        else:
            return False

    def get(self,key,value = None):
        try:
            return self[key]
        except Exception:
            return value
    
d = StrKeyDict0([('2', 'two'), ('4', 'four')])
print(d.get(1))
print(1 in d)
# print(d[1])


from types import MappingProxyType

tony_args = {
    'name':'tony',
    'age':'11',
    'sex':'man'
}

tony_proxy = MappingProxyType(tony_args)
print(tony_proxy)
try:
    tony_proxy['mother'] = 'mm'
except:
    print('报错')

tony_args['mother'] = 'mm'
print(tony_proxy)


class1 = {
    '1':'cai',
    '2':'tony',
    '3':'tom'
}
new_student = {
    '8':'jenny',
    '9':'poter'
}
class1.update(new_student)
print(class1)

class1.update([('1','caipeiyan'),(4,'yangliuqing')])
print(f'第二次打印：{class1}')